<?php

class	htmlHelper
{
  protected $tags = array(
			  "form" => "<form %s>",
			  "endform" => "</form>",
			  "input" => '<input %s />',
			  "link" => "<a %s>%s</a>",
			  "textarea" => "<textarea %s>%s</textarea>",
			  "checkbox" => "<input type='checkbox' %s />",
			  "button" => "<button %s>%s</button>",
			  "image" => "<img %s />",
			  "block" => "<div %s>%s</div>",
			  "css" => "<link %s />",
			  "charset" => "<meta http-equiv='Content-Type' content='text/html; %s'",
			  "javascript" => "<script type='text/javascript' %s>%s</script>",
			  "javascriptlink" => "<script type='text/javascript' %s></script>",
			  "tag" => "<%s %s>%s</%s>"
			  );

  private function tag($attr)
  {
    $append = "";
    $attrs = "";
    if (is_array($attr) and $attr != NULL)
      foreach ($attr as $key => $value)
	{
	  if ($key != "value" and $key != "tag")
	    $append .= "$key=\"$value\" ";
	}
    return sprintf($this->tags['tag'], isset($attr['tag']) ? $attr['tag'] : "", $append, isset($attr['value']) ? $attr['value'] : "", isset($attr['tag']) ? $attr['tag'] : "");
  }

  private function tagWithValue($tagname, $attr)
  {
    $append = "";
    $attrs = "";
    if (is_array($attr) and $attr != NULL)
      foreach ($attr as $key => $value)
	{
	  if ($key != "value")
	    $append .= "$key=\"$value\" ";
	}
    return sprintf($this->tags[$tagname], $append, isset($attr['value']) ? $attr['value'] : "");
  }

  private function tagWithoutValue($tagname, $attr)
  {
    $append = "";
    $attrs = "";
    if (is_array($attr) and $attr != NULL)
      foreach ($attr as $key => $value)
	{
	  $append .= "$key=\"$value\" ";
	}
    return sprintf($this->tags[$tagname], $append);
  }

  private function addTag($tagname, $attr = array())
  {
    $ret = "";
    if (isset($this->tags[$tagname])) {
      switch ($tagname) {
      case 'link':
      case 'textarea':
      case 'button':
      case 'block':
      case 'javascript':
	$ret = self::tagWithValue($tagname, $attr);
	break;
      case 'tag':
	$ret = self::tag($attr);
	break;
      default:
	$ret = self::tagWithoutValue($tagname, $attr);
      }
    }
    return $ret;
  }

  public function addForm($attr = array())
  {
    return self::addTag('form', $attr);
  }

  public function addInput($attr = array())
  {
    return self::addTag('input', $attr);
  }

  public function endForm()
  {
    return self::addTag('endform');
  }

  public function addlink($attr = array())
  {
    return self::addTag('link', $attr);
  }

  public function addImage($attr = array())
  {
    return self::addTag('image', $attr);
  }

  public function addTextarea($attr = array())
  {
    return self::addTag('textarea', $attr);
  }

  public function addJavascript($attr = array())
  {
    return self::addTag('javascript', $attr);
  }

  public function addHTMLTag($attr = array())
  {
    return self::addTag('tag', $attr);
  }

}

?>